#include <actionlib/client/simple_action_client.h>
#include <ros/ros.h>
#include <ros1_protobuf_msg_bridge/action/ActDemoAction.h>

void feedbackAct1(const ros1_protobuf_msg_bridge::ActDemoFeedbackConstPtr &feedback) {
    ROS_INFO("Dishes cleaned: %d", (int)feedback->val());
}

int main(int argc, char **argv) {
    ros::init(argc, argv, "act_client");

    actionlib::SimpleActionClient<ros1_protobuf_msg_bridge::ActDemoAction> ac1("t_act1", true);

    ROS_INFO("Waiting for t_act1 server to start.");
    ac1.waitForServer();

    ROS_INFO("Server t_act1 started, sending goal.");

    ros1_protobuf_msg_bridge::ActDemoGoal goal;
    goal.set_val(21);
    goal.set_key("4321");

    // 监听反馈
    ac1.sendGoal(goal, actionlib::SimpleActionClient<ros1_protobuf_msg_bridge::ActDemoAction>::SimpleDoneCallback(),
                 actionlib::SimpleActionClient<ros1_protobuf_msg_bridge::ActDemoAction>::SimpleActiveCallback(), &feedbackAct1);

    // 不需要监听反馈
    // ac1.sendGoal(goal);

    bool finished_before_timeout = ac1.waitForResult(ros::Duration(30.0));

    if (finished_before_timeout) {
        actionlib::SimpleClientGoalState state = ac1.getState();
        ROS_INFO("Action finished: %s", state.toString().c_str());
        ROS_INFO("Total dishes value: %d", (int)ac1.getResult()->val());
    } else
        ROS_INFO("Action did not finish before the time out.");
    return 0;
}
